package qumaimai.dao.impl;

import java.sql.Connection;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Statement;
import java.util.ArrayList;
import java.util.List;

import qumaimai.dao.FundDao;
import qumaimai.entity.Fund;
import qumaimai.entity.vo.CartVo;
import qumaimai.util.JdbcUtil;

public class FundDaoImpl implements FundDao {

	@Override
	public List<Fund> findByUserId(int userId) {
		List<Fund> list = new ArrayList<Fund>();
		String sql="select * from t_fund where user_id="+userId +" order by create_time desc";
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String createTime = rs.getString("create_time");
				int type = rs.getInt("type");
				double money = rs.getDouble("money");
				double balance = rs.getDouble("balance");
				String vipRemark = rs.getString("vip_remark");
				String adminRemark = rs.getString("admin_remark");
				int status = rs.getInt("status");				
				Fund fund = new Fund(id, createTime, type, money, balance, vipRemark, adminRemark, status, userId);
				list.add(fund);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int add(double money,double balance, String vipRemark, int userId) {
		String sql = "INSERT INTO t_fund (type, money, balance, vip_remark, status, user_id) VALUES (1, "+money+", "+balance+", '"+vipRemark+"',  3, "+userId+")";
		return JdbcUtil.update(sql);
	}

	@Override
	public List<Fund> findCheck(int page,int size) {
		int n = (page-1)*size;
		List<Fund> list = new ArrayList<Fund>();
		String sql="select * from t_fund where 1=1 limit "+n+","+size;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			while(rs.next()){
				int id = rs.getInt("id");
				String createTime = rs.getString("create_time");
				int type = rs.getInt("type");
				double money = rs.getDouble("money");
				double balance = rs.getDouble("balance");
				String vipRemark = rs.getString("vip_remark");
				String adminRemark = rs.getString("admin_remark");
				int status = rs.getInt("status");				
				Fund fund = new Fund(id, createTime, type, money, balance, vipRemark, adminRemark, status, 0);
				list.add(fund);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return list;
	}

	@Override
	public int getCount() {
		int n = 0;
		String sql = "select count(*) from t_fund where 1=1";
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int confirm(int id,double balance) {
		String sql = "update t_fund set status=1,balance=money+"+balance+" where id = "+id;
		System.out.println(sql);
		return JdbcUtil.update(sql);
	}

	@Override
	public double getBalance(int id) {
		double n = 0;
		String sql = "select balance from t_fund where id="+id;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getDouble(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public double getBlanaceByUserId(int userId) {
		double n = 0;
		String sql = "select balance from t_fund where status=1 and user_id="+userId+" order by create_time desc limit 1 ";
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getDouble(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int checkStatus(int userId) {
		int n = 0;
		String sql = "select count(*) from t_fund where status=3 and user_id="+userId;
		Connection conn = JdbcUtil.getConnection();
		Statement st;
		try {
			st = conn.createStatement();
			ResultSet rs = st.executeQuery(sql);
			if(rs.next()){
				n = rs.getInt(1);
			}
		} catch (SQLException e) {
			e.printStackTrace();
		}
		return n;
	}

	@Override
	public int pay(double money, double balance, int userId) {
		String sql = "INSERT INTO t_fund (type, money, balance, status, user_id) VALUES (2, "+money+", "+balance+",  1, "+userId+")";
		return JdbcUtil.update(sql);
	}


}
